// pages/home/cart.js
const api = require("../../request/api")
const request = require("../../request/request")
const App = getApp();
const util = require("../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 15,
    selected_list: [],
    buying_number: 1,
    cart_list: [{a:1}, {b:2}],
    uid: 9,
    total_price: 0,
    if_selected_all: true,
    number: 0,
    if_touching: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init_data()
    this.get_cart_list()
    this.get_goods_list()
  },
  init_data() {
    this.setData({
      uid: util.getAppValue(App.globalData.uid),
      if_touching: false
    })
  },
  get_goods_list() {
    let params = {
      page: this.data.page,
      pageSize: this.data.page_size,
      uid: this.data.uid 
    }
    request.post(api.goods_more, params).then(res=>{
      this.setData({
        good_list: res
      })
    })
  },
  go_home_button() {
    wx.switchTab({
      url: '/pages/home/home',

    })
  },
  change_button_color() {
    this.setData({
      if_touching: true
    })
  },
  reset_button_color() {
    this.setData({
      if_touching: false
    })
  },
  cart_add(){
    util.showLoading("正在添加")
    let params = {
      // goods_num: this.data.cart_num,
      goods_id: this.data.goods_id,
      uid: this.data.uid
    }
    request.post(api.cart_add, params).then(res=>{
      this.setData({
        if_show_box:false
      })
      util.showToast("已加入购物车")
    })
  },
  go_detail_from_component(e) {
    let id = e.detail.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id,
    })
  },
  go_detail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id,
    })
  },
  get_cart_list() {
    util.showLoading("正在加载")
    let params = {
      uid: this.data.uid
    }
    request.post(api.cart_list, params).then(res=>{
      util.hideLoading()
      this.setData({
        cart_list: res
      })
      if (this.data.selected_list.length == 0) {
        let selected_list = []
        for (let item in res) {
          selected_list.push(true)
        }
        this.setData({
          selected_list
        })
      }
      this.counting_price()
    })
  },
  get_select_list() {
    
  },
  counting_price() {
    let selected_list = this.data.selected_list
    let number = 0
    let total_price = 0
    for (let i in selected_list) {
      if (selected_list[i] == true & this.data.cart_list.length != 0) {
        let single_price = this.data.cart_list[i].sell_price * this.data.cart_list[i].goods_num
        total_price += single_price
        number += this.data.cart_list[i].goods_num
      }
    }
    this.setData({
      total_price,
      number
    })
  },
  minus(e) {
    let params = {
      uid: this.data.uid,
      goods_id: e.currentTarget.dataset.goods_id
    }
    request.post(api.cart_minus, params).then(res=>{
      this.get_cart_list()
    })

  },
  selecting(e) {
    let index = e.currentTarget.dataset.index
    let selected_list = this.data.selected_list
    selected_list[index] = !selected_list[index]
    this.setData({
      selected_list
    })
      this.counting_price()
      this.get_selected_all()
  },
  go_pay() {
    let url = '/pages/order/pay?type=' + '7'
    wx.navigateTo({
      url: url,
    })
  },
  
  all_picking() {
    this.setData({
      if_selected_all: !this.data.if_selected_all
    })
    let selected_list = []
    let if_selected = this.data.if_selected_all?true:false
    for (let i in this.data.cart_list){
      selected_list.push(if_selected)
    }
    this.setData({
      selected_list
    })
    this.counting_price()
  },

  plus(e) {
    let params = {
      uid: this.data.uid,
      goods_id: e.currentTarget.dataset.goods_id
    }
    request.post(api.cart_add, params).then(res=>{
      this.get_cart_list()
    })

  },
  cancel(e){
    util.showLoading('正在删除')
    let selected_list = this.data.selected_list
    let id = e.currentTarget.dataset.goods_id
    let index = e.currentTarget.dataset.index
    let params = {
      uid: this.data.uid,
      deleteGoods: [{goods_id: id}]
      // goods_id: e.currentTarget.dataset.goods_id,
      // goods_sku_id: 0
    }
    request.post(api.cart_delete, params).then(res=>{
      selected_list.splice(index,1)
      this.setData({
        selected_list
      })
      this.get_cart_list()
    })
  },
  get_selected_all() {
    let selected_list = this.data.selected_list
    for (let i of selected_list) {
      if (i == false) {
        this.setData({
          if_selected_all: false
        })
        return
      }
    }
    this.setData({
      if_selected_all: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.get_cart_list()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})